package controller

import (
	"github.com/gofiber/fiber"
	"gowork/newCustomers/model"
	"github.com/json-iterator/go"
	"strconv"
)

type NewCustomersController struct {
	DietInformation *model.DietInformation
}

func (ncc *NewCustomersController)Submit(c *fiber.Ctx){

	ncc.DietInformation = new(model.DietInformation)

	userIdStr := c.FormValue("userId")
	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	dietInformationListStr := c.FormValue("dietInformationList")
	var dietInformationList []*model.DietInformation
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(dietInformationListStr), &dietInformationList)

	accountStr := c.FormValue("account")
	var account model.Account
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(accountStr), &account)

	res , err := ncc.DietInformation.Submit(userId, dietInformationList, &account, dietInformationListStr)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (ncc *NewCustomersController)SubmitAsync(c *fiber.Ctx){

	ncc.DietInformation = new(model.DietInformation)

	userIdStr := c.FormValue("userId")
	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	dietInformationListStr := c.FormValue("dietInformationList")
	var dietInformationList []*model.DietInformation
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(dietInformationListStr), &dietInformationList)

	accountStr := c.FormValue("account")
	var account model.Account
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(accountStr), &account)

	res , err := ncc.DietInformation.SubmitAsync(userId, dietInformationList, &account, dietInformationListStr)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (ncc *NewCustomersController)ViewReport(c *fiber.Ctx){

	ncc.DietInformation = new(model.DietInformation)

	userIdStr := c.FormValue("userId")
	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := ncc.DietInformation.ViewReport(userId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (ncc *NewCustomersController)ViewReportAsync(c *fiber.Ctx){

	ncc.DietInformation = new(model.DietInformation)

	userIdStr := c.FormValue("userId")
	userId, err := strconv.ParseInt(userIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := ncc.DietInformation.ViewReportAsync(userId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

func (ncc *NewCustomersController)QueryDietInformation(c *fiber.Ctx){

	ncc.DietInformation = new(model.DietInformation)

	res , err := ncc.DietInformation.QueryDietInformation()
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": "200",
		"data": res,
		"message": "成功",
	})
	return
}

























